package hu.elte.inf.holiuml.gui;

import hu.elte.inf.holiuml.model.Attribute;
import hu.elte.inf.holiuml.model.Operation;
import hu.elte.inf.holiuml.model.UMLClass;

import javax.swing.*;

import java.awt.event.*;

import java.awt.*;
import java.util.Vector;

import hu.elte.inf.holiuml.model.ClassNode;
/**
 * @author Istvan Bozo
 */
public class ClassNodeOptionsDialog extends OptionsDialog{

	private JPanel propPanel;
	JTextField nameTextField;
	JCheckBox showOpCheckBox;
	JCheckBox showAttrCheckBox;
	ClassNode classNode;
	Vector<Attribute> attributes = new Vector<Attribute>();
	Vector<Operation> operations = new Vector<Operation>();

	public ClassNodeOptionsDialog(HoliUML parent, ClassNode classNode){
		super(parent, "Class Node Options");
		if(classNode != null){
			this.classNode = classNode;
		}
		createPropPanel();
		fillPanels();
		setSize(new Dimension(450,200));
		setModalityType(ModalityType.APPLICATION_MODAL);
		if(classNode != null){
			showAttrCheckBox.setSelected(classNode.getVisibleAttributes());
			showOpCheckBox.setSelected(classNode.getVisibleOperations());
			nameTextField.setText(classNode.getUMLClass().getName());
			for(int i = 0; i < classNode.getUMLClass().getAttributes().size(); i++){
				attributes.add(new Attribute(classNode.getUMLClass().getAttribute(i)));
			}
			for(int i = 0; i < classNode.getUMLClass().getOperations().size(); i++){
				operations.add(new Operation(classNode.getUMLClass().getOperationAt(i)));
			}
		}
		setVisible(true);
	}
	
	private void createPropPanel(){
		propPanel = new JPanel(new GridLayout(5,2));
		JLabel nameLabel = new JLabel("Name of the class:");
		//todo add existing names
		nameTextField = new JTextField();
		nameTextField.setEditable(true);
		JButton attrButton = new JButton("Edit attributes");
		attrButton.addActionListener(editAttributeAction);
		JButton operationButton = new JButton("Edit operation");
		operationButton.addActionListener(editOperationAction);
		showOpCheckBox = new JCheckBox("",true);
		showAttrCheckBox = new JCheckBox("",true);
		//adding to the panel
		propPanel.add(new JLabel("Attributes of the class:"));
		propPanel.add(nameTextField);
		propPanel.add(new JLabel("Attributes of the class:"));
		propPanel.add(attrButton);
		propPanel.add(new JLabel("Methods of the class:"));
		propPanel.add(operationButton);
		propPanel.add(new JLabel("Show attributes of the class?"));
		propPanel.add(showAttrCheckBox);
		propPanel.add(new JLabel("Show opeartions of the class?"));
		propPanel.add(showOpCheckBox);
		
	}
	private void fillPanels(){
		getContentPane().setLayout(new BorderLayout());
		getContentPane().add("North", propPanel);
		getContentPane().add("South", buttonPanel);
	}
	
	public ClassNode createData(){
		if(classNode == null){
			UMLClass umlClass = new UMLClass();
			super.parent.getDocument().getModel().addClass(umlClass);
			umlClass.setName(nameTextField.getText());
			umlClass.setAttribute(attributes);
			umlClass.setOperation(operations);
			classNode = new ClassNode();
			classNode.setVisibleAttributes(showAttrCheckBox.isSelected());
			classNode.setVisibleOperations(showOpCheckBox.isSelected());
			classNode.setUMLClass(umlClass);
		}else{
			classNode.getUMLClass().setName(nameTextField.getText());
			classNode.getUMLClass().setAttribute(attributes);
			classNode.setVisibleAttributes(showAttrCheckBox.isSelected());
			classNode.setVisibleOperations(showOpCheckBox.isSelected());
		}
		return classNode;
	}
	
	public void modifyData(ClassNode classNode){
		classNode.getUMLClass().setName(nameTextField.getText());
		classNode.getUMLClass().setAttribute(attributes);
		classNode.getUMLClass().setOperation(operations);
		classNode.setVisibleAttributes(showAttrCheckBox.isSelected());
		classNode.setVisibleOperations(showOpCheckBox.isSelected());
	}
	
	private ActionListener editAttributeAction = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			ClassAttributeOptionsDialog attrDialog;
				 attrDialog = new ClassAttributeOptionsDialog(parent, "Edit attributes", attributes);
			if (attrDialog.getButtonCode() == OptionsDialog.OK) {
				attributes = attrDialog.getAttributes();
			}
		}
	};
	
	private ActionListener editOperationAction = new ActionListener() {
		public void actionPerformed(ActionEvent e) {
			ClassOperationOptionsDialog operationDialog;
			if(classNode == null){
				 operationDialog = new ClassOperationOptionsDialog(parent, "Edit operations", operations);
			}else{
				operationDialog = new ClassOperationOptionsDialog(parent, "Edit operations", classNode.getUMLClass().getOperations());
			}
			if (operationDialog.getButtonCode() == OptionsDialog.OK) {
				operations = operationDialog.getOperations();
			}
		}
	};
	
	protected boolean processOK(){
		if(nameTextField.getText().replaceAll(" ", "").equals("")){
			JOptionPane.showMessageDialog(parent, "Please fill the class name!", "Error!", JOptionPane.WARNING_MESSAGE);
			return false;
		}
		return true;
	}
	protected void processCancel(){}
}
